home *** CD-ROM | disk | FTP | other *** search
- /* LOGDISP.C Novell Utility
- Purpose: Program to Display a File if a certain amount of time has
- passed since the last login.
- Usage - To limit system information/news files to once a day displays
- Input - 1st Parameter is time passed in minutes
- 2nd Parameter is file spec of file to be displayed
- 3rd Parameter is optional wait time after display
- Output- Will display text file if time duration has passed or file date
- is newer than the last login date.
-
- * Author - Wm Stackpole, Code 114.4, Puget Sound Naval Shipyard
- for the public domain
- * Date - 08/29/90
- * Written in MSC v5.1
- * Requires Novell Netware C Interface-DOS
- * Command line: cl /AM logdisp.c /link MNIT.LIB
- */
-
- /* Portions of this code are the copyrighted property of William Stackpole
- and Precision Data Consultants. Used with permission.
- */
-
- /* Update Log */
- /* v1.1 Creates LST$LOG.DAT file because LAST_LOGIN property couldn't be used */
- /* Added -n No Update option for LST$LOG.DAT file */
-
- /* Notes
- Suggested Parameters
- -cNN Number of columns for word wrap
- -lNN Number of lines for pause
- */
-
- #include <stdio.h>
- #include <ctype.h>
- #include <dos.h>
- #include <string.h>
- #include <nit.h>
- #include <niterror.h>
- #include <time.h>
- #include <conio.h>
- #include <bios.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- #define inc "Puget Sound Naval Shipyard, Wm Stackpole, 1990"
- #define prg "LOGDISP"
- #define ver "1.1"
-
- #define FALSE 0
- #define TRUE 1
- #define DEBUG FALSE
- #define MAXDUR 32768 /* Maximum duration 3+ weeks */
-
- union REGS Regs;
- struct SREGS Segs;
-
- /* Prototype functions */
-
- int NAttach(void); /* See if user is attached to Novell network */
- void Help(void); /* Display help */
- long int DateToMinutes(int,int,int,int,int); /*Convert date to minutes */
- int WaitOne(int); /* Pause with a timeout */
- int IsOption(char*); /*Test of argv option */
- void ErrExit(int,int); /*Exit with Error Message */
- void ExitThisPrgm(int); /*Exit with Error Code */
- void DisplayFile(FILE*,int,int); /*Display a file with word wrap and page pause */
-
- /* Global variables */
-
- char *HelpMess[] ={
- "-Display a file if a certain amount of time has passed",
- "since the user's last login.",
- " Usage: LOGDISP [nnn] [d:][\\path\\]filename.ext [-w[tt]] [-n]",
- "Where nnn is the time to have passed in minutes (Default is 240)",
- "d: is the drive letter or volume name, \\path\\ is the directory",
- "path and filename.ext is the name of the text file, -n prevents",
- "login time update, -w is a program pause of 20 seconds (or key press)",
- "and tt is an optional pause time in seconds (0 = wait for key press).",
- "Returns ERRORLEVEL 1 for fatal errors.",
- inc,
- };
- int HelpLen=10;
-
- char *ErrorMess[]={
- "Unknown option.",
- "Bad or missing command line parameter.",
- "Invalid time parameter (1-32768).",
- "Opening display file.",
- "Bad or missing file specification.",
- "Opening LST$LOG.DAT file.",
- "Reading LST$LOG.DAT file.",
- "Writing LST$LOG.DAT file.",
- };
- int ErrLen=8;
-
- int completionCode;
- BYTE securityAccessLevel;
- long objectID;
- char objectName[48];
- WORD objectType;
- char propertyName[16];
- int segmentNumber=1;
- BYTE propertyValue[128];
- BYTE moreSegments;
- BYTE propertyFlags;
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
-
- int haveDuration=FALSE;
- int haveFileName=FALSE;
- int duration=240;
- int updateLog=TRUE;
-
- long int currentTime, lastLogTime=0;
- int i;
- FILE *fp;
- char dateAndTime[7];
- char ans[8];
- static char fileName[128]="\0";
- static char filePath[128]="\0";
- char c;
- struct stat buf;
- int fh, result;
- struct tm *newtime;
- int waitTime=-1;
- int pageWidth=-1;
- int pageLength=-1;
-
- /* Parse the arguments */
- if(argc<2)
- Help();
- for(i=1;i<argc;i++) /* Scan each arguement for option, duration, file */
- {
- if(IsOption(argv[i]))
- {
- switch(tolower(argv[i][1]))
- {
- case 'w':
- if(strlen(argv[i])>2){
- strcpy(dateAndTime,&argv[i][2]);
- waitTime = atoi(dateAndTime);
- }
- else
- waitTime = 20;
- break;
- case 'c': /* page width in columns */
- if(strlen(argv[i])>2){
- strcpy(dateAndTime,&argv[i][2]);
- pageWidth = atoi(dateAndTime)<15?15:atoi(dateAndTime);
- pageWidth = pageWidth>132?132:pageWidth;
- }
- else
- pageWidth = 80;
- break;
- case 'l': /* page length option */
- if(strlen(argv[i])>2){
- strcpy(dateAndTime,&argv[i][2]);
- pageLength = atoi(dateAndTime)<8?8:atoi(dateAndTime);
- pageLength = pageLength>120?120:pageLength;
- }
- else
- pageLength = 23;
- break;
- case 'n': /* no update of LST$LOG.DAT file */
- updateLog=FALSE;
- break;
- default :
- ErrExit(0,1);
- }
- }
- else /* Test for file name parameter */
- {
- if((strchr(argv[i],'\\')!=NULL) || (strchr(argv[i],':')!=NULL) ||
- (strchr(argv[i],'.')!=NULL))
- { /* Assume a file spec */
- if(haveFileName)
- ErrExit(1,1);
- else
- {
- strcpy(fileName,argv[i]);
- haveFileName=TRUE;
- }
- }
- else
- { /* Assume it's a duration parameter */
- if(haveDuration)
- ErrExit(1,1);
- else
- {
- duration=atoi(argv[i]);
- haveDuration=TRUE;
- }
- }
- }
- #if DEBUG
- printf("Wait=%i Duration=%i Filespec=%s \n",waitTime,duration,fileName);
- #endif
-
- }
-
- /* Test validity of parsed parameters */
- if(strlen(fileName)==0)
- ErrExit(4,1);
- if((duration==0) || (duration>MAXDUR)) /* Test for maximum value on timer */
- ErrExit(2,1);
-
- i=NAttach(); /* See if we're attached to Network */
-
- if(i!=0)
- { /* Exit with error if not attached */
- printf("%s v%s %s\nThis utility requires Advance Netware to run.\n",
- prg,ver,inc);
- ExitThisPrgm(1);
- }
- /* Convert object ID and put in file specification string */
- sprintf(filePath,"SYS:MAIL\\%lX\\LST$LOG.DAT\0",objectID);
-
- GetFileServerDateAndTime(dateAndTime); /* Get the current date and time */
- currentTime=DateToMinutes(dateAndTime[0]+1900,dateAndTime[1],dateAndTime[2],
- dateAndTime[3],dateAndTime[4]); /* Convert it to minutes */
-
- #if DEBUG
- printf("Current Date %i/%i/%i, %i:%i Minutes=%li\n",dateAndTime[0],
- dateAndTime[1],dateAndTime[2],dateAndTime[3],dateAndTime[4],
- currentTime);
- printf("File path is %s\n",filePath);
- #endif
-
- /* Last Login is useless since it is updated prior to login script execution */
- /* We will need to save this info in the user's mailbox */
- /* Filename = LST$LOG.DAT */
- /* Contents will be a long Minutes of the last login */
-
-
- result = stat(filePath,&buf); /* Get the file date and time */
-
- #if DEBUG
- if(result!=0)
- printf("ERROR - Unable to read LST$LOG.DAT file information\n");
- #endif
-
- fp = fopen(filePath,"rb");
- if(fp!=NULL){
- completionCode=fread(&lastLogTime,sizeof(long),1,fp);
- if(completionCode==0){
- printf("ERROR - %s\n",ErrorMess[6]);
- lastLogTime=0;
- }
-
- #if DEBUG
- if(fp==NULL)
- printf("ERROR - %s\n",ErrorMess[5]);
- #endif
-
- }
-
- /* Write current time into file and close */
- if(updateLog){
- fp = fopen(filePath,"wb");
- if(fp==NULL)
- printf("ERROR - %s\n",ErrorMess[5]);
- else{
- completionCode=fwrite(¤tTime,sizeof(long),1,fp);
- if(completionCode==0)
- printf("ERROR - %s\n",ErrorMess[7]);
- }
- }
- #if DEBUG
- printf("Log %li + Duration %i = %li > %li\n",lastLogTime,duration,
- lastLogTime+duration,currentTime);
- #endif
-
- if((lastLogTime+duration)>currentTime) /* Test for duration has passed */
- {
- result = stat(fileName,&buf); /* Get the file date and time */
- if(result!=0)
- printf("ERROR - Unable to read file information\n");
- else{
- newtime = localtime(&buf.st_atime);
- currentTime = DateToMinutes(newtime->tm_year+1900,newtime->tm_mon+1,
- newtime->tm_mday,newtime->tm_hour,newtime->tm_min);
- #if DEBUG
- printf("File Date %s\n",ctime(&buf.st_atime));
- printf("Log %li < File %li\n",lastLogTime,currentTime);
- #endif
- if(lastLogTime>currentTime) /* Compare to last login date */
- ExitThisPrgm(0);
- }
- }
-
- /* Display the file */
- fp=fopen(fileName,"r");
- if(fp==NULL)
- ErrExit(3,1);
-
- DisplayFile(fp, pageWidth, pageLength);
-
- fclose(fp);
- if(waitTime!=-1)
- WaitOne(waitTime);
- ExitThisPrgm(0);
- }
-
- /* FUNCTION DateToMinutes - Convert a date to minutes since 1980
- Input - Year, month, day, hour, minute
- Format - YYYY >1970 , MM( 1-12), DD, HH (0-23), MM (0-59)
- Output - long int minutes passed since 1980
- */
- long int DateToMinutes(int year,int month,int day,int hour,int minute)
- {
- static int daysPassed[13]={0,0,31,59,90,120,151,181,212,243,273,304,334};
-
- year -= 1970;
- day += daysPassed[month]-(!(year%4 || month<3)?0:1);
-
- return((long)minute+60*((long)hour+24*((long)day+(year/4)+(year%4?1:0)+
- (long)year*365)));
- }
-
- /* FUNCTION NAttach - Tests for attachment to network */
- /* Output - Errorlevel 0 if shell loaded and user is logged in */
- /* Errorlevel 1 if shell is not loaded */
- /* Errorlevel 2 if shell loaded and user is not logged in */
- /* Errorlevel 99 if user is supervisor */
- int NAttach(void)
- {
-
- completionCode = GetBinderyAccessLevel(&securityAccessLevel,&objectID);
- if (completionCode !=0) /* Shell Not Loaded */
- return(1);
-
- completionCode = GetBinderyObjectName(objectID,objectName,&objectType);
- if (completionCode != 0) /* Not logged in */
- return(2);
-
- return(0);
- }
-
- /* FUNCTION Help - Displays the help message and exits */
- void Help(void)
- {
- int i;
- printf("\n%s v%s ",prg,ver);
- for(i=0;i<HelpLen;i++)
- puts(HelpMess[i]);
- exit(1);
- }
-
- /* FUNCTION WaitOne - Waits n seconds before continuing execution
- Input - Time to wait in seconds (1 to 120) 0=Forever
- Output - 0 = Time expired 1=User pressed key
- */
- int WaitOne(int seconds)
- {
- int dflt_time = 6;
- int dflt_limit = 120;
- long int start_time, cur_time;
- int i=0;
- static char mess1[]="\nPress any key to continue...";
-
- cputs(mess1);
- if(seconds!=0){
- seconds = seconds<1?dflt_time:seconds;
- seconds = seconds>dflt_limit?dflt_limit:seconds;
- start_time = time(&start_time)+seconds;
- while(start_time>time(&cur_time))
- {
- if(_bios_keybrd(1)!=0){
- getch();
- i = 1;
- break;
- }
- }
- }
- else{
- i = 1;
- getch();
- }
- putchar('\n');
- return(i);
- }
-
- /* FUNCTION IsOption - Tests for command line options
- Input - argv to be tested
- Output - TRUE or FALSE
- */
- int IsOption(char *instring)
- {
- return((instring[0]=='/' || instring[0]=='-')?TRUE:FALSE);
- }
-
- /* FUNCTION ErrExit - Display an Error Message and Exit with Errorlevel
- Input - Error message number and errorlevel number
- Output - ERRORLEVEL to DOS process
- */
- void ErrExit(int errMessage,int errLevel)
- {
- if(errMessage<ErrLen)
- printf("ERROR - %s\n%s v%s %s\n",ErrorMess[errMessage],
- prg,ver,inc);
- ExitThisPrgm(errLevel);
- }
-
- /* FUNCTION ExitThisPrgm - Exit program with Errorlevel
- Input - Errorlevel
- Output - ERRORLEVEL to DOS process
- */
- void ExitThisPrgm(int Errorlevel)
- {
- exit(Errorlevel);
- }
-
- /* FUNCTION DisplayFile - Display a file with work wrap and page pause
- Input - File pointer, page width and page length
- Output - File to stdout (Returns nothing)
- */
- void DisplayFile(FILE *fp,int width,int length)
- {
- char lineBuffer[255];
- char ch;
- int haveWrap = width==-1?FALSE:TRUE;
- int havePause= length==-1?FALSE:TRUE;
- int i;
-
- ch = fgetc(fp);
- while(!(feof(fp)))
- {
- /* Read in a line or fill buffer */
-
- ch=fgetc(fp);
- putchar(ch);
- }
- return;
- }